﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LybrixNursingCRMDesktop.FrameWork;
using LybrixNursingCommon;
using System.Data.Objects.SqlClient;
namespace LybrixNursingCRMDesktop.Landing
{
    /// <summary>
    /// Interaction logic for LandingPage.xaml
    /// </summary>
    public partial class LandingPage : Page
    {
        private NursingDBContainer _entities;
        private customer currentCustomer = null;
        public LandingPage()
        {
            InitializeComponent();
            _entities = Repository.Entities;
            bool isInitial = false;

            if ((bool)Login.loggedUser.isAgent && (!(bool)(Login.loggedUser.isSuperUser) || !(bool)(Login.loggedUser.isAdmin)))
                isInitial = true;
            PerformSearch(isInitial);
        }








        private void PerformSearch(bool isInitial)
        {
            IQueryable<Teek> teeks = null;

            teeks = (from x in _entities.Teeks
                     where (x.status != "3" && x.status != "4" &&  SqlFunctions.DateDiff("day",DateTime.Now, x.follow_date) == 0)
                     select x);

            if (isInitial)
            {
                if ((bool)Login.loggedUser.isAgent && (!(bool)Login.loggedUser.isSuperUser || !(bool)Login.loggedUser.isAdmin))
                {
                    teeks = teeks.Where(x => x.agent.Id == Login.loggedUser.Id);
                }
            }
            
            ListViewOfFollowUps.ItemsSource = teeks;
            if (teeks != null)
                followUpCount.Content = teeks.Count().ToString();
            else
                followUpCount.Content = "0";
        }

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var dep = (DependencyObject)e.OriginalSource;
            while ((dep != null) && !(dep is ListViewItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }
            if (dep == null)
                return;
            currentCustomer = ((Teek)ListViewOfFollowUps.ItemContainerGenerator.ItemFromContainer(dep)).customer;
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            NavigationCenter.ShowDetailPage(currentCustomer, NavigationTarget.TeekDetails, this);
            e.Handled = true;
        }




    }
}
